package com.atguigu.spring6.tx.service;

import com.atguigu.spring6.tx.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Transactional 参数说明
 * 1、只读 @Transactional(read-only=true)
 * 2、超时 @Transactional(timeout = 3)
 * 3、回滚策略
 *      @Transactional(noRollbackFor = {ArithmeticException.class})
 *      @Transactional(noRollbackForClassName =)
 *      @Transactional(rollbackFor =)
 *      @Transactional(rollbackForClassName = )
 * 4、隔离级别
 *      @Transactional(isolation = )
 * 5、传播级别
 *      @Transactional(propagation = )
 */

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Transactional()
    @Override
    public void buyBook(Integer bookId, Integer userId) {
        // 查询图书价格
        Integer bookPriceByBookId = bookDao.getBookPriceByBookId(bookId);
        // 更新库存
        bookDao.updateStock(bookId);
        // 更新用户余额
        bookDao.updateUserBalance(userId, bookPriceByBookId);
    }
}
